package com.cchao.simplelib.http.intercaptor;

import android.util.Log;

import com.cchao.simplelib.LibCore;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.util.StringHelper;
import com.google.gson.Gson;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 网络请求的 日志收集拦截器
 *
 * @author cchao
 * @version 2019/4/9.
 */
public class RequestLogInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        try {
            HttpUrl httpUrl = request.url();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            Log.e("发送请求req", "method--->" + request.method());
            //收集请求体
            if (request.body() instanceof FormBody) {
                FormBody formBody = ((FormBody) request.body());
                for (int i = 0; i < formBody.size(); i++) {
                    map.put(URLDecoder.decode(formBody.encodedName(i), "UTF-8"), URLDecoder.decode(formBody.encodedValue(i), "UTF-8"));
                }
            }
            //收集url参数
            for (String str : request.url().queryParameterNames()) {
                map2.put(str, request.url().queryParameterValues(str).get(0));
            }
            //data_cipher 这是key
            Logs.logEvent(LibCore.getInfo().getAppId() + " 发送请求req", "Url[" + httpUrl.url() + "]");
            Logs.logEvent(LibCore.getInfo().getAppId() + " 发送请求req", "Body[" + map + ']');
            Logs.logEvent(LibCore.getInfo().getAppId() + " 发送请求req", "header[" + request.headers() + ']');

            if (!map.isEmpty()) {
                //清空原始body
                builder.delete();
                //配置新的body
                FormBody.Builder body = new FormBody.Builder();
                String mBody = AesUtil.encrypt(new Gson().toJson(map));
                Logs.logEvent(LibCore.getInfo().getAppId() + " 发送请求req", "加密后Body[" + mBody + ']');
                body.add("data_cipher", mBody);
                builder.put(body.build());
            }

            //如果url里面有参数则生成新的加密参数url
            if (!map2.isEmpty()) {
                String mBody2 = AesUtil.encrypt(new Gson().toJson(map2));
                String newPath = httpUrl
                        .newBuilder("?data_cipher=" + mBody2)
                        .build()
                        .toString();
                if (request.isHttps()) {
                    newPath = newPath.replace("+", "%20");
                } else {
                    newPath = newPath.replace("+", "%2B");
                }
                Logs.logEvent(LibCore.getInfo().getAppId() + " 发送请求req", "NewUrl[" + newPath + "]");
                builder.url(newPath);
            }
        } catch (Exception e) {
            Logs.logException(e);
        }
        return chain.proceed(request);
    }
}
